<?php
class SeatSoldsController extends SeatAppController {
	
	public function beforeFilter(){
		$this->Auth->allow('index','seatTable','seatCart');
		parent::beforeFilter();
	}
	
	 public function index($ticket_id){
	 	$this->loadModel('Seat.TicketShowing');
	 	$seatTicketShowing = $this->TicketShowing->findById($ticket_id);
	 	
	 	$this->loadModel('Seat.SeatTicket');
	 	$this->SeatTicket->recursive = 2;
	 	$seatTicket = $this->SeatTicket->findById($seatTicketShowing['TicketShowing']['seat_ticket_id']);
	 	
	 	$this->loadModel('Ticket');
	 	$this->Ticket->recursive = 1;
	 	$ticket = $this->Ticket->find('first',array(
 			'fields'=>array('Ticket.id','Ticket.title','TicketCate.*','TicketScate.*'),
 			'conditions'=>array(
 				'Ticket.id'=>$seatTicket['SeatTicket']['ticket_id']
 			)
	 	));
	 	$this->set(compact('ticket','seatTicket','seatTicketShowing'));
	 }
	 
	 public function seatTable($table_id,$seat_ticket_id,$ticket_showing_id = false){
	 	$recursive = 5;
	 	$this->loadModel('Seat.ModelX');
	 	$this->ModelX->ModelTable->ModelTr->ModelTd->bindModel(array(
 			'hasOne'=>array(
 				'Seat'=>array(
 					'className' => 'Seat.Seat',
 					'foreignKey' => 'td_id',
 					'conditions' => array(
 						'Seat.seat_ticket_id'=>$seat_ticket_id,
 						'Seat.enable'=>1
 					),
 				)
 			)
	 	));
	 	$this->ModelX->ModelTable->ModelTr->ModelTd->Seat->bindModel(array(
	 			'hasOne'=>array(
	 					'SeatSold'=>array(
	 							'className' => 'Seat.SeatSold',
	 							'foreignKey' => 'ticket_seat_id',
	 							'conditions' => array(
	 									'SeatSold.ticket_showing_id'=>$ticket_showing_id,
	 							),
	 					)
	 			)
	 	));
	 	$table = $this->ModelX->ModelTable->find('first',array(
	 			'conditions'=>array('ModelTable.id'=>$table_id),
	 			'recursive'=>$recursive
	 	));
	 	$state = true;
	 	$data = array(
 			'w'=>$table['ModelTable']['width'],
 			'h'=>$table['ModelTable']['height']
 		);
	 	$this->set(compact('table','state','data'));
	 }
	
	 public $cart = array(
	 		'data'=>array(),
	 		'price'=>0,
	 		'count'=>0,
	 		'seat'=>true
	 );
	 
 	private function refreshCart(){
 		$count = $price = 0;
 		foreach ($this->cart['data'] as $item) {
 			$count+=$item['count'];
 			if($item['param']==1){
 				$price+=$item['count']*$item['eprice'];
 			}else{
 				$price+=$item['count']*$item['price'];
 			}
 		}
 		$this->cart['count'] = $count;
 		$this->cart['price'] = $price;
 	}
 	
	public function seatCart(){
 		if ($this->request->is('post')) {
 			$item = $this->request->data;
 			
 			$ticket_id = $item['ticket_id'];
 			$seat_ticket_id = $item['seat_ticket_id'];
 			$ticket_showing_id = $item['ticket_showing_id'];
 			
 			$this->loadModel('Ticket');
 			$this->Ticket->recursive = 1;
 			$ticket = $this->Ticket->find('first',array(
 					'fields'=>array('Ticket.id','Ticket.title','TicketCate.*','TicketScate.*'),
 					'conditions'=>array(
 							'Ticket.id'=>$ticket_id
 					)
 			));
 			$this->loadModel('Seat.TicketShowing');
 			$seatTicketShowing = $this->TicketShowing->findById($ticket_showing_id);
 			
 			$this->Session->delete(Configure::read("Cart.SessionKey"));
 			foreach ($item['SeatSold'] as $ss){ 
 				$ticket['TicketShowing'] = $seatTicketShowing['TicketShowing'];
	 			$id = $ss['seat_id'];
	 			if(isset($this->cart['data']['seat_'.$id])){
	 				unset($this->cart['data']['seat_'.$id]);
	 			}
 				//$obj = $this->getObject($id);
 				$this->cart['data']['seat_'.$id] = array(
 						'id'=>$id,
 						'title'=>$ss['title'],
 						'price'=>$ss['price'],
 						'eprice'=>0,
 						'count'=>1,
 						'object'=>$ticket,
 						'param'=>$ss,
 						'seat'=>true
 				);
 			}
 			$this->refreshCart();
 			$data = $this->cart['count'];
 			$this->Session->write(Configure::read("Cart.SessionKey"),$this->cart);
 			$msg = '添加成功';
 			$state = true;
 		}else{
 			$msg = 'no operator';
 			$state = false;
 		}
 		$this->set(compact('state','msg','data'));
 		$this->render(false);
	 }
}